import { popTarget, pushTarget } from "./dep";
import Dep from "./dep"
import { quereWatcher } from "./scheduler";
let id = 0
export default class Watcher {
  constructor(vm, fn) {
    this.vm = vm;
    this.deps = []
    this.depsId = new Set();
    this.id = id++
    // 为什么叫getter？ updateComponent >> render >> 模版/数据 >> 虚拟dom
    this.getter = fn
    this.get()
  }

  get() {
    console.log('更新试图')
    pushTarget(this)
    this.getter()
    popTarget()
  }

  addDep(dep) {
    const id = dep.id
    if (!this.depsId.has(id)) {
      this.deps.push(dep)
      this.depsId.add(id)
      // console.log(this.deps
      dep.addSub(this)
    }
  }

  update() {
    
    // this.get()
    quereWatcher(this)
  }
}